package com.shen.msmservice.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.google.gson.JsonObject;
import com.shen.msmservice.service.MsmService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Map;

@Service
public class MsmServiceImpl implements MsmService {

    @Override
    public boolean send(Map<String, Object> param, String phone) {
        //从redis获取验证码 获取不到 进行发送


        if(StringUtils.isEmpty(phone)) return false;
        DefaultProfile profile =
        DefaultProfile.getProfile("default", "LTAI4GGfSwqESnS2qVSvHBq7", "F39lmLujNjZAFj50MN6IWiw2brZuQv");
        IAcsClient client = new DefaultAcsClient(profile);
        CommonRequest request = new CommonRequest();
        //request.setProtocol(ProtocolType.HTTPS);
        //设置固定参数
        request.setMethod(MethodType.POST);
        request.setDomain("dysmsapi.aliyuncs.com");
        request.setVersion("2017-05-25");
        request.setAction("SendSms");//请求的那个方法
        //设置相关参数
        request.putQueryParameter("PhoneNumbers", phone); //必须写为PhoneNumbers
        request.putQueryParameter("SignName", "我的谷粒学院在线教育网站");//申请的签名名称
        request.putQueryParameter("TemplateCode", "SMS_194910924");//模板code
        request.putQueryParameter("TemplateParam", JSONObject.toJSONString(param));//要求传递的数据为json格式---map变为json（fastJson） 验证码数据

        //最终发送
        try{
            CommonResponse response = client.getCommonResponse(request);
            boolean success = response.getHttpResponse().isSuccess();
            return success;
        }
        catch (Exception e){
            e.printStackTrace();
        }

        return false;
    }
}
